package com.baozun.houji.me.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.network.mCommonApiService
import com.baozhun.mall.common.util.LoginHelper
import com.baozhun.mall.common.util.onkeylogin.OneKeyLoginHelper
import com.baozhun.mall.common.widget.dialog.CommonDialog
import com.baozun.houji.me.R
import com.baozun.houji.me.network.mMeApiService
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/12/9
 * @Version：1.0.0
 */
class LoginViewModel : BaseViewModel() {
    var phone = MutableLiveData("")
    var verifyCode = MutableLiveData("")
    var remainingTime = MutableLiveData<Int>()
    var agreeProtocol = MutableLiveData<Boolean>()
    var currentPhone = MutableLiveData<String>()
    var operatorName = MutableLiveData<String>()

    /**
     * 获取验证码
     */
    fun getVerifyCode(getSuccess: () -> Unit) {
        checkParams {
            request({ mCommonApiService.getVerifyCode(phone.value, DeviceUtils.getAndroidID()) }, {
                getSuccess.invoke()
            }, isShowDialog = true)
        }

    }

    /**
     * 邮箱登陆
     */
    fun login(operateSuccess: () -> Unit) {
        checkParams {
            checkProtocol {
                request({ mMeApiService.login(phone.value.toString(), verifyCode.value.toString()) }, { it ->
                    LoginHelper.getInstance().loginSuccess(it) {
                        operateSuccess.invoke()
                    }
                }, isShowDialog = true, showSuccessMsg = false)
            }
        }
    }

    private fun checkParams(checkVerifyCode: Boolean = false, checkPass: () -> Unit) {
        if (phone.value.isNullOrEmpty()) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_input_phone)
            return
        }
        if (checkVerifyCode && verifyCode.value.isNullOrEmpty()) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_input_pwd)
            return
        }
        if (!RegexUtils.isMobileSimple(phone.value)) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_input_correct_phone)
            return
        }
        checkPass.invoke()
    }

    /**
     * 检测协议是否选中
     */
    private fun checkProtocol(checkPass: () -> Unit) {
        if (agreeProtocol.value == true) {
            checkPass.invoke()
        } else {
            CommonDialog(ActivityUtils.getTopActivity()) {
                agreeProtocol.value = true
                checkPass.invoke()
            }.title(com.baozhun.mall.res.R.string.warm_reminder)
                .content(OneKeyLoginHelper.getInstance().getProtocolSpannableString("请先阅读并同意履行"))
                .cancelText(com.baozhun.mall.res.R.string.close)
                .confirmText(com.baozhun.mall.res.R.string.read_and_agree)
                .isClickableSpan(true)
                .dismissOnTouchOutside(false)
                .show()
        }

    }

}